import scrapy
import re
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
from mySpiders.utils import parse_img_url
import logging
logger = logging.getLogger(__name__)


class AmazonSpider(scrapy.Spider):
    name = 'amazon'
    allowed_domains = ['amazon.com']
    start_urls = ['https://www.amazon.com/Best-Sellers-Electronics-Audio-Headphones/zgbs/electronics/172541/ref=zg_bs_nav_e_1_e']

    def parse(self, response):
        url_main = "https://www.amazon.com"
        lis = response.xpath("//ol[@id='zg-ordered-list']/li")
        for li in lis:
            # 判断商品是否可售
            try:
                is_sale = li.xpath(".//span[@class='a-size-large aok-inline-block zg-item-unavailable zg-text-center-align']/text()").extract_first()
                if is_sale:
                    continue
            except:
                pass
            # 链接
            href = url_main + li.xpath(".//span[@class='aok-inline-block zg-item']/a[@class='a-link-normal']/@href").extract_first()
            SKU_ID = re.search(r".*/dp/(\w+)", href).group(1)
            # 排名
            sort_by = li.xpath(".//span[@class='zg-badge-text']/text()").extract_first()
            # sort_by = "#%02d" % (lis.index(li) + 1)
            # 标题
            title = li.xpath(".//div[@class='a-section a-spacing-small']/img/@alt").extract_first().replace("'", "")
            # title = title.replace("【", "").replace("】", "")
            # 图片
            img_url = li.xpath(".//div[@class='a-section a-spacing-small']/img/@src").extract_first()
            meta_dict = {}
            meta_dict['name'] = self.name
            meta_dict['img_url'] = img_url
            meta_dict['sort_by'] = sort_by
            meta_dict['title'] = title
            yield scrapy.Request(href, callback=self.parse_detail, meta=meta_dict)
        # 下一页
        next_url = response.xpath('//a[text()="Next page"]/@href').extract_first()
        yield scrapy.Request(next_url, callback=self.parse)

    def parse_detail(self, response):
        item = response.meta
        item['rating'] = response.xpath('//*[@id="acrCustomerReviewText"]/text()').extract_first()
        print(item)
        return item